package com.feng.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.feng.entity.RestResponse;
import com.feng.entity.vo.AccountForgotVo;
import com.feng.entity.vo.AccountRegisterVo;
import com.feng.entity.vo.LoginCodeVo;
import com.feng.exception.SystemException;
import com.feng.service.AccountService;
import com.feng.utils.Const;
import com.feng.utils.HttpCodeEnum;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/api/auth")
public class AuthorizationController {

    CircleCaptcha circleCaptcha;

    final
    AccountService accountService;

    final
    StringRedisTemplate redisTemplate;


    public AuthorizationController(AccountService accountService, StringRedisTemplate redisTemplate) {
        this.accountService = accountService;
        this.redisTemplate = redisTemplate;
    }

    @GetMapping("/getCaptcha")
    public RestResponse  chickCode() throws IOException {
        circleCaptcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 20);
        String imageBase64 = "data:image/png;base64," + circleCaptcha.getImageBase64();
        String key = UUID.randomUUID().toString();
        redisTemplate.opsForValue().set(Const.LOGIN_VERIFY_CODE + key, circleCaptcha.getCode(), Const.TWO, TimeUnit.MINUTES);
        return RestResponse.success(new LoginCodeVo(key, imageBase64));
    }

    @GetMapping("/verifyCaptcha/{captcha}/{key}")
    public RestResponse verifyCaptcha(@PathVariable String captcha, @PathVariable String key) {
        if(!StringUtils.hasText(captcha) || !StringUtils.hasText(key)) throw new SystemException(HttpCodeEnum.CODE_IS_NOLL);
        String code = redisTemplate.opsForValue().get(Const.LOGIN_VERIFY_CODE + key);
        if(!StringUtils.hasText(code)) throw new SystemException(HttpCodeEnum.CODE_IS_NOLL);
        if(!code.equals(captcha)) throw new SystemException(HttpCodeEnum.CODE_IS_ERROR);
        redisTemplate.delete(Const.LOGIN_VERIFY_CODE + key);
        return RestResponse.success();
    }

    @GetMapping("/forgotCode")
    public RestResponse forgotCode(String username, HttpServletRequest request){
        return accountService.forgotCode(username, request);
    }

    @PostMapping("/forgot")
    public RestResponse forgot(@RequestBody AccountForgotVo forgotVo) {
        return accountService.forgot(forgotVo);
    }

    @PostMapping("/changePassword")
    public RestResponse changePassword(@RequestBody AccountForgotVo forgotVo) {
        return accountService.changePassword(forgotVo);
    }


    @GetMapping("/sendEmail")
    public RestResponse sendEmail(String username, HttpServletRequest request) {
        return accountService.sendEmail(username, request);
    }

    @PostMapping("/register")
    public RestResponse register(@RequestBody AccountRegisterVo registerVo) {
        return accountService.register(registerVo);
    }

}
